﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.Admin;




namespace WindowsForms
{
    public partial class FormLogIn : Form
    {
        public FormLogIn()
        {
            InitializeComponent();
        }
      
       
   
        private int dem = 0;
        private void btXExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormLogIn_Load(object sender, EventArgs e)
        {
           
            
        }

        private void btXDangNhap_Click(object sender, EventArgs e)
        {
            
            if (txtUserName.Text == "" && txtPassword.Text == "")
            {
                MessageBox.Show("Nhập Username và Password");
            }

            else
            {
                if (txtUserName.Text == "")
                {
                    MessageBox.Show("Nhập Username ");
                }
                else
                {
                    if (txtPassword.Text == "")
                    {
                        MessageBox.Show("Nhập Password ");
                    }
                    else
                    {

                        // string pass = Encode.Coded(txtPassword.Text);
                        string pass = txtPassword.Text;
                        //     string kq = User.KiemTraUser(txtUserName.Text, pass);
                        //string kq = "";
                        //TaiKhoan kq = TaiKhoan.Objects.Find(c => c.UserName == txtUserName.Text && c.Password == pass).Single();
                        TaiKhoan kq = TaiKhoan.Objects.GetById(txtUserName.Text);

                        if (kq != null && kq.Password == pass && kq.TrangThai)
                        {
                            FormMain frm = new FormMain(kq);
                            this.Visible = false;
                            frm.ShowDialog();

                            this.Visible = true;

                            //QuyenHan=Us

                        }
                        else
                        {
                            dem++;
                            MessageBox.Show("Đăng nhập không thành công");
                            //ClearText();
                        }
                    }
                }
            }
            if (dem == 3)
            {
                this.Close();
            }
        }
        
       
            
            
    
        private void FormLogIn_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btXDangNhap.PerformClick();
        }

             
    }
}
